#include "BestQuoteDeal.h"

#include <core/StyleMgr.h>
#include <widgets/SImageButton.h>
#include <widgets/STabCtrl.h>

#include <QHBoxLayout>
#include <QStylePainter>
#include <QTimer>
#include <QVBoxLayout>

#include "../BondUtility.h"
#include "../RealTimeDeal/RealDealTable.h"
#include "BestQuoteDlg.h"

BestQuoteDeal::BestQuoteDeal(BestQuoteDlg* parent)
    : QFrame(parent), m_pParent(parent) {
  QString style =
      QString(
          "BestQuoteDeal {background-color:#%1;border:none;border-radius:2px;}")
          .arg(StyleMgr::instance().color("BasicPlate").rgb(), 0, 16);
  setStyleSheet(style);

  connect(m_pParent, &BestQuoteDlg::filterChange, this,
          &BestQuoteDeal::onFilterChange);

  QVBoxLayout* mainLayout = new QVBoxLayout(this);
  mainLayout->setContentsMargins(0, 4, 0, 0);
  mainLayout->setSpacing(8);

  QHBoxLayout* titleLayout = new QHBoxLayout();
  titleLayout->setContentsMargins(4, 0, -4, 0);
  titleLayout->setSpacing(0);
  mainLayout->addLayout(titleLayout);

  m_collapseBtn = new SImageButton(this, "widgets_dropdown_down");
  m_collapseBtn->setToolTip(tr("隐藏"));
  connect(m_collapseBtn, &SImageButton::clicked, [=]() {
    setCollapsed(!m_collapsed);
    emit collapsed(m_collapsed);
  });
  titleLayout->addWidget(m_collapseBtn);
  titleLayout->addStretch();

  QPushButton* btnDeal = new QPushButton(tr("成交统计"), this);
  btnDeal->setFixedSize(80, 24);
  btnDeal->hide();
  titleLayout->addWidget(btnDeal);

  m_brokerTab = BondUtility::createBrokerTab(this, true, false);
  if (m_brokerTab) {
    mainLayout->addWidget(m_brokerTab, 0, Qt::AlignCenter);
    m_brokerTab->selectTab(0);

    connect(m_brokerTab, &STabCtrl::tabSelected, this,
            &BestQuoteDeal::onBrokerTabChanged2);
  }

  m_table = new RealDealTable(this);
  m_table->setEnviFilterPtr(&m_envi, &m_blh);
  mainLayout->addWidget(m_table);

  QTimer::singleShot(100, [=]() { initUi(); });
}

void BestQuoteDeal::setCollapsed(bool v) {
  m_collapsed = v;
  m_collapseBtn->setImgId(m_collapsed ? "widgets_dropdown_up"
                                      : "widgets_dropdown_down");
  m_collapseBtn->setToolTip(m_collapsed ? tr("显示") : tr("隐藏"));
  m_brokerTab->setVisible(!m_collapsed);
  m_table->setVisible(!m_collapsed);
}

void BestQuoteDeal::onBrokerTabChanged(int index) {
  if (0 == index) {
    m_brokerTab->selectTab(index);
  } else {
    m_brokerTab->selectTab(index - 1);
  }

  changeBroker();
}

void BestQuoteDeal::onBrokerTabChanged2(int index) {
  m_brokerTab->selectTab(index);
  changeBroker();
}

void BestQuoteDeal::changeBroker() {
  int corpId = m_brokerTab->tabData(m_brokerTab->getSelTab()).toInt();
  bool showAll = false;  // m_nCorpID == 0 ? true :
                         // false;//取消全部tab点击后也重置最后更新排序
  if (m_envi.m_nFilterType == FilterType::FilterType_NONE) {
    m_table->onFilterChange(true);
    m_table->changeParam(showAll, corpId);
  } else if (m_envi.m_nFilterType == FilterType::FilterType_Bond) {
    m_table->onFilterChange(false);
    m_table->changeParam(showAll, corpId);
  } else if (m_envi.m_nFilterType == FilterType::FilterType_Search) {
    m_table->onFilterChange(false);
    m_table->changeParam(showAll, corpId);
  } else if (m_envi.m_nFilterType == -1) {
    BLHCONDITION blh;
    FMTBUF(blh.sCompanyid, "%d", corpId);
    blh.nHasFlg = 0;
    m_blh = blh;
    m_table->onFilterChange(true);
    m_table->changeParam(showAll, corpId);
  } else if (m_envi.m_nFilterType == FilterType::FilterType_BondAndCondition) {
    m_table->onFilterChange(true);
    FilterCondition tmp;
    m_table->setFilterCondition(&tmp);
    m_table->changeParam(showAll, corpId);
  }
}

void BestQuoteDeal::onFilterChange() {
  CBrokerUnit brokerUnit = m_pParent->getCurBroker();
  FilterCondition& fcTemp = m_pParent->getFilterCondition();
  if (fcTemp.strSearch == "") {
    if (fcTemp.m_nFocusType == 2) {
      QString strGroupID;  // TODO
      fcTemp.strGroupID = strGroupID;
      changeParam(brokerUnit.m_brokerID,
                  FilterType::FilterType_BondAndCondition, &fcTemp, true);
    } else {
      changeParam(brokerUnit.m_brokerID, FilterType::FilterType_NONE,
                  &fcTemp.stBlh, true);
    }
  } else {
    changeParam(brokerUnit.m_brokerID, FilterType::FilterType_Search, &fcTemp,
                true);
  }
}

void BestQuoteDeal::initUi() {
  if (!m_pParent->selValidCompany()) return;
  onFilterChange();
}

void BestQuoteDeal::changeParam(int brokerId, int nType, void* pFilter,
                                bool bShowAll) {
  m_table->setFilterCondition(nullptr);
  if (nType == FilterType::FilterType_NONE) {
    BLHCONDITION blh;
    blh = *((BLHCONDITION*)pFilter);
    // if(m_blh == blh&&nType == m_envi.m_nFilterType)
    //	return;
    // else
    m_blh = blh;

    m_envi.m_nFilterType = nType;
    m_table->onFilterChange(true);
    m_table->changeParam(bShowAll, brokerId);
  } else if (nType == FilterType::FilterType_Bond) {
    m_envi.m_strId = *((QString*)pFilter);
    m_envi.m_nFilterType = nType;
    m_table->onFilterChange(false);
    m_table->changeParam(bShowAll, brokerId);

  } else if (nType == FilterType::FilterType_Search) {
    m_envi.m_nFilterType = nType;
    m_table->onFilterChange(false);
    m_table->setFilterCondition((FilterCondition*)pFilter);
    m_table->changeParam(bShowAll, brokerId);
  } else if (nType == -1) {
    BLHCONDITION blh;
    FMTBUF(blh.sCompanyid, "%d", brokerId);
    blh.nHasFlg = 0;
    m_blh = blh;
    m_envi.m_nFilterType = nType;
    m_table->onFilterChange(true);
    m_table->changeParam(bShowAll, brokerId);
  } else if (nType == FilterType::FilterType_BondAndCondition) {
    m_envi.m_nFilterType = nType;
    m_table->onFilterChange(true);
    m_table->setFilterCondition((FilterCondition*)pFilter);
    m_table->changeParam(bShowAll, brokerId);
  }
}

void BestQuoteDeal::paintEvent(QPaintEvent* e) {
  QFrame::paintEvent(e);
  QRect r = rect();
  r.adjust(0, 2, 0, 0);
  r.setHeight(24);

  QStylePainter painter(this);
  QFont f = font();
  f.setPixelSize(14);
  painter.setFont(f);
  painter.setPen(StyleMgr::instance().color("TextNormal"));
  painter.drawText(r, tr("当日成交"), QTextOption(Qt::AlignCenter));
}
